package nieAS.display {
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import nieAS.html.Navigate;
	import nieAS.ui.Mouse;
	
	public class NieStage extends MovieClip {
		protected static var _Root:NieStage;
		
		//页面状态
		protected static var isSetRightMouse:Boolean = true;
		protected static var isSetNavigate:Boolean = true;
		
		protected static var _stageWidth_first:int = 1000;
		private static var _stageWidth_end:int;
		private static var _stageX_begin:int;
		private static var _stageX_end:int;
		
		private static var _resizeList:Vector.<Function> = new Vector.<Function>();
		
		/**
		 * _stageWidth_first 默认宽度:1000
		 * isSetRightMouse 设置鼠标右键
		 * isSetNavigate 设置JS链接
		 */
		public function NieStage():void {
			if (_Root == null) {
				_Root = this;
			} else {
				throw Error("主场景已存在");
			}
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP;
			
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event = null) {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			if (isSetRightMouse) {
				Mouse.setMouseRightClick(this);
			}
			
			if (isSetNavigate) {
				try {
					Navigate.linkReady();
				} catch (e:Error) {
					
				}
			}
			
			this.stage.addEventListener(Event.RESIZE, function() {
					checkStageWidth();
					for (var i:int = 0; i < _resizeList.length; i++) {
						_resizeList[i]();
					}
				});
			
			checkStageWidth();
			for (var i:int = 0; i < _resizeList.length; i++) {
				_resizeList[i]();
			}
			
			startUp();
		}
		
		protected function startUp():void {
			throw new Error("必须override方法startUp");
		}
		
		/**
		 * 返回场景实际左端X
		 */
		public static function get stageX_begin():int {
			return _stageX_begin;
		}
		
		/**
		 * 返回场景实际右端X
		 */
		public static function get stageX_end():int {
			return _stageX_end;
		}
		
		/**
		 * 场景resize时触发方法
		 * @param	_fun
		 */
		public static function pushResize(_fun:Function):void {
			_resizeList.push(_fun);
		}
		
		private function checkStageWidth():void {
			_stageWidth_end = this.stage.stageWidth;
			_stageX_begin = (_stageWidth_first - _stageWidth_end) / 2;
			_stageX_end = _stageX_begin + _stageWidth_end;
		}
	}
}